<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "chat".
 *
 * @property string $id
 * @property string $name 会话名称，当是群时：群聊名称
 * @property int $type 群类型0：好友，1：普通群
 * @property int $user1 会话人1
 * @property int $user2 会话人2
 * @property int $lasttime 最近消息时间
 * @property string $addtime 创建时间
 */
class Chat extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'chat';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['addtime'], 'required'],
            [['lasttime', 'addtime'], 'integer'],
            [['name'], 'string', 'max' => 100],
            [['type'], 'string', 'max' => 2],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '会话名称，当是群时：群聊名称',
            'type' => '群类型0：好友，1：普通群',
            'user1' => '会话人1',
            'user2' => '会话人2',
            'lasttime' => '最近消息时间',
            'addtime' => '创建时间',
        ];
    }
    
    public static function getUserFriendsMsg($userid){
        $sql = "select * from chat where user1=$userid or user2=$userid order by lasttime desc";
        $command = \Yii::$app->db->createCommand($sql);
        $chats = $command->queryAll();
        $data = [];
        $total_unreadnum = 0;
        foreach($chats as $key => $chat){
            $chats[$key]['lastmsg'] = ChatMsg::getLastMsg($chat['id']);
            $unreadnum = ChatMsg::getUnreadNum($chat['id'],$userid);
            $chats[$key]['unreadnum'] = $unreadnum;
            $total_unreadnum += $unreadnum;
            
            $chats[$key]['friendid'] = $userid == $chat['user1'] ? $chat['user2'] : $chat['user1'];
            $friend = User::findOne(['id'=>$chats[$key]['friendid']]);
            $chats[$key]['friend_headimg'] = empty($friend->headimg) ? '/img/user/default-head.png' : $friend->headimg;
            $chats[$key]['friend_name'] = empty($friend) ? 'unknown' : $friend->username;
            
            $chats[$key]['lasttime'] = Tools::calculateTimelong($chat['lasttime']);
        }
        $data['chats'] = empty($chats) ? [] : $chats;
        $data['total_unreadnum'] = $total_unreadnum;
        return $data;
    }
    
    public static function updateLastTime($chatid){
        $now = time();
        $sql = "update chat set lasttime=$now where id=$chatid";
        $command = \Yii::$app->db->createCommand($sql);
        $command->execute();
    }
    
//    public static function getChat($you,$me){
//        $sql_1 = "select * from chat where user1=$you and user2=$me";
//        $sql_2 = "select * from chat where user1=$me and user2=$you";
//        
//        $command = \Yii::$app->db->createCommand($sql_1);
//        $exist1 = $command->queryOne();
//        $command = \Yii::$app->db->createCommand($sql_2);
//        $exist2 = $command->queryOne();
//        
//        if(empty($exist1) && empty($exist2) ){
//            
//        }
//    }
}
